package com.bifrost.config;

import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.orm.jpa.JpaProperties;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.orm.jpa.EntityManagerFactoryBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.orm.hibernate4.LocalSessionFactoryBean;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.orm.jpa.vendor.Database;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import javax.persistence.EntityManager;
import javax.sql.DataSource;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

/**
 * @author rangf
 * @date 2018/2/24
 */
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(
        entityManagerFactoryRef="cbEntityManagerFactory",
        transactionManagerRef="transactionManagerSecondary",
        basePackages= { "com.bifrost.cbrepository" })
public class CbestRepositoryConfig {

    @Autowired
    private JpaProperties jpaProperties;
    @Autowired
    @Qualifier("cbDataSource")
    private DataSource cbDataSource;

    @Bean(name = "cbEntityManager")
    public EntityManager cbEntityManager(EntityManagerFactoryBuilder builder) {
        return cbEntityManagerFactory(builder).getObject().createEntityManager();
    }

    @Bean(name = "cbEntityManagerFactory")
    public LocalContainerEntityManagerFactoryBean cbEntityManagerFactory (EntityManagerFactoryBuilder builder) {
        return builder
                .dataSource(cbDataSource)
                .properties(getVendorProperties(cbDataSource))
                .packages("com.bifrost.cbentity")
                .persistenceUnit("cbPersistenceUnit")
                .build();
    }

    private Map<String, String> getVendorProperties(DataSource dataSource) {
        jpaProperties.setDatabase(Database.ORACLE);
        Map<String,String> map = new HashMap<>();
        map.put("hibernate.dialect","org.hibernate.dialect.Oracle10gDialect");
        map.put("hibernate.hbm2ddl.auto","none");
        jpaProperties.setProperties(map);
        return  jpaProperties.getHibernateProperties(dataSource);
    }

    @Bean(name = "transactionManagerSecondary")
    PlatformTransactionManager transactionManagerPrimary(EntityManagerFactoryBuilder builder) {
        return new JpaTransactionManager(cbEntityManagerFactory(builder).getObject());
    }
   /*@Bean(name = "cbSessionFactory")
   @Qualifier("cbSessionFactory")
   @ConfigurationProperties(prefix = "spring.jpa.second")
   public SessionFactory cbSessionFactoryBean() {
       try {
           LocalSessionFactoryBean lsfb = new LocalSessionFactoryBean();
           lsfb.setDataSource(cbDataSource());
           lsfb.setPackagesToScan("com.bifrost.cbentity");
           Properties props = new Properties();
           props.setProperty("hibernate.show_sql", "true");
           props.setProperty("hibernate.hbm2ddl.auto", "update");
           props.setProperty("hibernate.dialect", "org.hibernate.dialect.MySQLDialect");
           props.setProperty("hibernate.format_sql", "true");
           lsfb.setHibernateProperties(props);
           lsfb.afterPropertiesSet();
           SessionFactory object = lsfb.getObject();
           return object;
       } catch (IOException e) {
           return null;
       }
   }*/
}
